home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C ++ / Applications / FlyThrough 1.1.2 / src / Source / Q3AutoObject.h < prev    next >
Encoding:
Text File  |  1997-03-17  |  1.3 KB  |  68 lines  |  [TEXT/CWIE]

  1. //
  2. //    Q3AutoObject.h
  3. //
  4. //    A template class for "smart pointer" QD3D *shared* objects.
  5. //
  6. //    By James Jennings
  7. //    March 8, 1997
  8. //
  9.  
  10. #pragma once
  11.  
  12. #define explicit
  13.  
  14. template<class T>    // can we restrict T to subclasses of TQ3Object?
  15. class Q3AutoObject {
  16. public:
  17.     explicit Q3AutoObject(T inObj = nil) : mObject(inObj)
  18.     { Assert_(inObj==nil || ::Q3Object_IsType(inObj, kQ3ObjectTypeShared) == kQ3True); }
  19.     
  20. //    template <class U>
  21.     Q3AutoObject(Q3AutoObject<T> &rhs) : mObject(rhs.GetRef()) {}    // copy constructor
  22.         
  23.     ~Q3AutoObject() { Release(); }
  24.     
  25. //    template <class U>
  26.     Q3AutoObject<T> operator=(Q3AutoObject<T> &rhs)
  27.     {
  28.         if (this != &rhs) Reset(rhs.GetRef());
  29.         return *this;
  30.     }
  31.     
  32. /*    Q3AutoObject<T> operator=(T rhs)    // use explicit casts instead
  33.     {
  34.         Assert_(rhs==nil || ::Q3Object_IsType(rhs, kQ3ObjectTypeShared) == kQ3True);
  35.         if (mObject != rhs) Reset(rhs);
  36.         return this;
  37.     }*/
  38.     
  39.     T Get() const { return mObject; }
  40.     T GetRef() const 
  41.     { 
  42.         if (mObject)
  43.             return ::Q3Shared_GetReference(mObject);
  44.         else
  45.             return nil;
  46.     }
  47.     // So we can pass this to QD3D
  48.     operator T() const { return Get(); }
  49.     
  50.     T Release()
  51.     {
  52.         T oldObject = mObject;
  53.         ::Q3Object_Dispose(mObject);
  54.         mObject = nil;
  55.         return oldObject;
  56.     }
  57.     
  58.     void Reset( T inObj = nil )
  59.     {
  60.         if (mObject != nil)
  61.             ::Q3Object_Dispose(mObject);
  62.         mObject = inObj;
  63.     }
  64.     
  65. private:
  66.     TQ3Object    mObject;
  67. };
  68.